var EventEmitter = require('events').EventEmitter;

var life = new EventEmitter();
// 设置监听器的最大个数
// 默认情况下： 事件监听器不要超过10个，太多将导致内存泄露
life.setMaxListeners(20);

life.on('showMsg', function (who) {
    console.log('show ' + who );

});
life.on('showMsg', function (who) {
    console.log('show 1' + who );

});
life.on('showMsg', function (who) {
    console.log('show 2' + who );

});
life.on('showMsg', function (who) {
    console.log('show 3' + who );

});
life.on('showMsg', function (who) {
    console.log('show 4' + who );

});
life.on('showMsg', function (who) {
    console.log('show 5' + who );

});
life.on('showMsg', function (who) {
    console.log('show ' + who );

});
life.on('showMsg', function (who) {
    console.log('show 1' + who );

});
life.on('showMsg', function (who) {
    console.log('show 2' + who );

});
life.on('showMsg', function (who) {
    console.log('show 3' + who );

});
life.on('showMsg', function (who) {
    console.log('show 4' + who );

});
life.on('showMsg', function (who) {
    console.log('show 5' + who );

});

life.on('ReadMsg', function (who) {
    console.log('ReadMsg 1' + who );

});

life.on('ReadMsg', show);
// 这个是具名函数，匿名函数不起任何作用
function show() {
    console.log('ReadMsg 2' + who );
}
// 单个移除事件
life.removeListener('ReadMsg', show);
// 移除所有事件
// life.removeAllListeners();
// 移除指定的一系列事件
life.removeAllListeners('showMsg');

var hasShowedListener = life.emit('showMsg', 'your message');
var hasReadListener = life.emit('ReadMsg', 'message');


console.log(life.listeners('ReadMsg').length);
console.log(life.listeners('showMsg').length);
console.log(EventEmitter.listenerCount(life, 'ReadMsg'));
console.log(EventEmitter.listenerCount(life, 'showMsg'));


console.log(hasShowedListener);
console.log(hasReadListener);
